#include <vector>

using namespace std;


class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int n = nums.size();
        int back = n-1;
        for (int i=n-1; i>=0; i--) {
            if (nums[i] == val) {
                nums[i] = nums[back--];
            }
        }
        return back+1;
    }
};
